package com.glory.brepda.activity.technicalservice;


import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.AbnormalMaintenanceAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.AbnormalMaintenanceBean;
import com.glory.brepda.entity.EqpKeepInfoBean;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import butterknife.BindView;

public class AbnormalMaintenanceActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.flCheckItem)
    FrameLayout flCheckItem;
    @BindView(R.id.tvCheckItem)
    TextView tvCheckItem;
    @BindView(R.id.ivCheckItem)
    ImageView ivCheckItem;
    @BindView(R.id.rvCheckItemList)
    RecyclerView rvCheckItemList;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.tvQuery)
    TextView tvQuery;

    private List<String> mCheckItemList = new ArrayList<>();
    private FreeUI_GeneralPop mCheckItemPop;
    private AbnormalMaintenanceAdapter adapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_abnormal_maintenance;
    }

    @Override
    protected void initView() {

        setMainLayout(llMain);
        tvTitle.setText("保养异常处置");
        tvBaseSubmit.setVisibility(View.GONE);

        initRecyclerView(rvCheckItemList, this);
        adapter = new AbnormalMaintenanceAdapter(null,AbnormalMaintenanceActivity.this);
        adapter.isFirstOnly(true);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvCheckItemList.setAdapter(adapter);
    }

    @Override
    protected void initData() {

        queryMaintenanceContent();

        tvQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                queryMessage();
            }
        });


        flCheckItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mCheckItemPop != null) {
                    if (mCheckItemPop.isShowing()) {
                        mCheckItemPop.dismiss();
                    }
                } else {
                    mCheckItemPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flCheckItem.getWidth(), mCheckItemList, ivCheckItem, (codeS, i) -> {
                        tvCheckItem.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivCheckItem);
                mCheckItemPop.showAsDropDown(flCheckItem, 0, 0);

            }
        });


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK) {
            queryMessage();
        }
    }


    private void queryMessage(){

        FreeUI_EntityCallBack<BaseResponseBean<AbnormalMaintenanceBean>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<AbnormalMaintenanceBean>>
                (new TypeToken<BaseResponseBean<AbnormalMaintenanceBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<AbnormalMaintenanceBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    adapter.setNewData(response.body().getResponse().getBody().getXPMSABNORMALHANDLEFORMS());
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<AbnormalMaintenanceBean>> response) {
                super.onError(response);
                loadError(response.getException(), "PMSABNORMALHANDLEFORMEXCUTE");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "QUERY");
        map.addProperty("EQPID", getText(etEqpID));
        map.addProperty("LOTID", "");
        map.addProperty("PMSSHEETNAME", tvCheckItem.getText().toString());
        NetUtils.requestPostNet("IF.PMSABNORMALHANDLEFORMEXCUTE", "Common", AbnormalMaintenanceActivity.this, map, callback);


    }


    private void queryMaintenanceContent() {

        FreeUI_EntityCallBack<BaseResponseBean<EqpKeepInfoBean>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpKeepInfoBean>>
                (new TypeToken<BaseResponseBean<EqpKeepInfoBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onSuccess(Response<BaseResponseBean<EqpKeepInfoBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {

                    response.body().getResponse().getBody().getxPmsPeriods().forEach(new Consumer<EqpKeepInfoBean.XPmsPeriodsDTO>() {
                        @Override
                        public void accept(EqpKeepInfoBean.XPmsPeriodsDTO xPmsPeriodsDTO) {
                            mCheckItemList.add(xPmsPeriodsDTO.getNAME());
                        }
                    });

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpKeepInfoBean>> response) {
                super.onError(response);
                loadError(response.getException(), "PMSSCHEDULE");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("actionType", "queryPmsPeriod");
        NetUtils.requestPostNet("IF.PMSSCHEDULE", "Common", AbnormalMaintenanceActivity.this, map, callBack);

    }


}